#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QDir>
#include <QtGui>
#include <QtCore>
#include <QtSQL>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionLoad_tab_File_triggered()
{
    //-------------------------取得輸入法清單------------------------//
    tabFile = QFileDialog::getOpenFileName(this,
                                           tr("get a liu table file"),
                                           QDir::homePath(),
                                           tr("Text files (*.txt)"));
    QFile liuTab(tabFile);
    //-------------------------讀取檔案---------------------------//
    if(! liuTab.open(QIODevice::ReadOnly)){
        QMessageBox::about(this,tr("Couldn't open table file"),tr("SqlTools couldn't upload liu Table file"));
    }
    else{
        QMap<QString,QString> Unicode;
        QTextStream in(&liuTab);
        in.readLine((qint64)80); //略過第一行
        while(!in.atEnd()){
                QString a = in.readLine((qint64)64);
                QString key = a.section("",0,5).simplified(),
                        word = a.section('~',1,1),
                        unicode = a.section('~',2,2);
                bool standard ;
                //-------------有趣的是，QString::Compare是傳回 0 代表字串相同----//
                //(a.section('~',3,3).compare("v") == 0)? standard = true: standard = false;
                if (!a.section('~',3,3).compare("v")) standard = true; else standard = false;
                bool simplify ;
                if (!a.section('~',4,4).compare("v")) simplify = true; else simplify = false;
                if(Unicode.contains(unicode))
                {
                    QStringList tmp;
                    tmp = dictionary.value(word);
                    tmp << key;
                    dictionary.insert(word,tmp);
                    if(standard){
                        tmp = Standard.value(word);
                        tmp << key;
                        Standard.insert(word,tmp);
                    }
                    if(simplify){
                        tmp = Simplify.value(word);
                        tmp << key;
                        Simplify.insert(word,tmp);
                    }
                }
                else
                {
                    Unicode.insert(unicode,word);
                    QStringList tmp;
                    tmp.append(key);
                    dictionary.insert(word,tmp);
                    if(standard)
                        Standard.insert(word,tmp);
                    if(simplify)
                        Simplify.insert(word,tmp);
                }
                //---------------------------------字根反查--------------------------------//
                if(Etymon.contains(key)){
                    QStringList tmp;
                    tmp = Etymon.value(key);
                    tmp.append(word);
                    Etymon.insert(key,tmp);
                }
                else{
                    QStringList tmp;
                    tmp.append(word);
                    Etymon.insert(key,tmp);
                }
            }
        liuTab.close();
        ui->label->setText(tr("Loading Finish"));
    }
}

void MainWindow::on_actionExport_Sqlitedb_File_triggered()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    sqlFile = QFileDialog::getSaveFileName(this,
                                           tr("Export to a SQLite database"),
                                           QDir::homePath(),
                                           tr("SQLite database (*.sqlitedb)"));
    db.setDatabaseName(sqlFile);
    if(db.open()){
        QSqlQuery q("", db);
        q.exec("create table Complete_key ( word varchar primary key, key varchar)");
        q.exec("create table Standard_key ( word varchar primary key, key varchar)");
        q.exec("create table Simplify_key ( word varchar primary key, key varchar)");
        q.exec("create table Etymon_words ( key varchar primary key, word varchar)");
        foreach(QString str , dictionary.keys())
            q.exec("insert into Complete_key values ('" + str + "' , '" + dictionary.value(str).join("/")  + "' )");
        foreach(QString str , Standard.keys())
            q.exec("insert into Standard_key values ('" + str + "' , '" + Standard.value(str).join("/")  + "' )");
        foreach(QString str , Simplify.keys())
            q.exec("insert into Simplify_key values ('" + str + "' , '" + Simplify.value(str).join("/")  + "' )");
        foreach(QString str , Etymon.keys())
            q.exec("insert into Etymon_words values ('" + str + "' , '" + Etymon.value(str).join("/")  + "' )");
        db.commit();
        db.close();
        ui->label->setText(tr("Exporting finish"));;
    }
    else{
        QMessageBox::about(this,tr("Couldn't open SQL Database"),tr("SqlTools couldn't Export SQL Database"));
    }
}
